<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Category;
use App\Models\Commodity;

class CommodityController extends Controller
{
    use ModelForm;
    /**
        * Index interface.
        *
        * @return Content
        */
       public function index()
       {
           return Admin::content(function (Content $content) {

               $content->header('商品');
               $content->description('管理');
               $content->body($this->grid());

           });
       }

       /**
        * Edit interface.
        *
        * @param $id
        * @return Content
        */
       public function edit($id)
       {
           return Admin::content(function (Content $content) use ($id) {

               $content->header('商品');
               $content->description('编辑');

               $content->body($this->form()->edit($id));
           });
       }

       /**
        * Create interface.
        *
        * @return Content
        */
       public function create()
       {
           return Admin::content(function (Content $content) {

               $content->header('商品');
               $content->description('创建');

               $content->body($this->form());
           });
       }

       /**
        * Make a grid builder.
        *
        * @return Grid
        */
       protected function grid()
       {
           return Admin::grid(Commodity::class, function (Grid $grid) {
               $grid->id('商品id');
               $grid->name('商品名称');
               $grid->price('商品价格');
               $grid->img('商品图片')->image();
               // $grid->introduce('商品简述');
               $grid->column('introduce','商品简述')->display(function ($introduce) {
                 $data =  str_replace("&nbsp;", "", htmlspecialchars_decode($introduce));
                 return $introduce;
               });

               $grid->category_id('商品分类id');
               $grid->stock('商品库存量');
               $grid->phone_number('电话');
               $grid->sales_volume('商品销量');
               // $grid->is_hot('是否热销');
               //热销设置
               $is_hot = [
               'on' => ['value' => '1', 'text' => '热销', 'color' => 'success'],
               'off' => ['value' => '0', 'text' => '取消热销', 'color' => 'danger'],
               ];
               $grid->column('is_hot','热销')->switch($is_hot);
               //上架设置
               $is_on_shelf = [
               'on' => ['value' => '1', 'text' => '上架', 'color' => 'success'],
               'off' => ['value' => '0', 'text' => '下架', 'color' => 'danger'],
               ];
               $grid->column('is_on_shelf','上架')->switch($is_on_shelf);

               $grid->filter(function($filter){

                   $filter->disableIdFilter();

                   $filter->where(function ($query) {

                       $query->whereHas('Category', function ($query) {
                           $query->where('name', 'like', "%{$this->input}%");
                       });
                   }, '分类');

                   $filter->like('name', '分类名称');

               });
               $grid->disableFilter();
               $grid->disableExport();
               // $grid->disableCreateButton();
               // $grid->disablePagination();
               $grid->disableFilter();
               $grid->paginate(10);
               $grid->perPages([10,20,30]);
               // $grid->disableRowSelector();
               // $grid->disableCreateButton();
               // $grid->disableActions();
               $grid->actions(function ($actions) {
                   // $actions->disableDelete();
                   // $actions->disableDelete();
               });

           });
       }

       /**
        * Make a form builder.
        *
        * @return Form
        */
       protected function form()
       {
           return Admin::form(Commodity::class, function (Form $form) {

               // $form->select('category_id', '商品分类')->options(Category::all()->pluck('full_name', 'id'));
               $form->select('category_id', '商品分类')->options(Category::pluck('name', 'id'));
               $form->text('name', '商品名称');
               $form->text('price', '商品价格');
               $form->image('img', '商品图片');
               // $form->text('introduce', '商品简述');
               $form->textarea("introduce","商品简述...")->rows(10);
               // $form->editor("introduce","商品简述");
               // $form->editor("introduce","商品简述...");
               // $form->text('category_id', '商品分类id');
               $form->text('stock', '商品库存量');
               $form->text('phone_number', '电话');
               // $form->text('sales_volume', '商品销量');
               $states = [
    'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
    'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
  ];
               $form->switch("is_hot","开启热销")->states($states);
               // $form->text('is_hot', '是否热销');

               //上架设置
               $is_on_shelf = [
               'on' => ['value' => '1', 'text' => '上架', 'color' => 'success'],
               'off' => ['value' => '0', 'text' => '下架', 'color' => 'danger'],
               ];
               $form->switch("is_on_shelf","上架")->states($states);
           });
       }
   }
   // $form->switch($column[, $label])->states($states);
   // $form->textarea($column[, $label])->rows(10);
